package ${packageName};

import com.dean.system.entity.${tableInfo.firstUpperEntityName};
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author dean
* @version 1.0
* @description: ${tableInfo.tableComment} Dao层
* @date ${dateTime}
*/
@Mapper
public interface ${tableInfo.firstUpperEntityName}Dao {

    /**
    * 新增
    * @param entity
    * @return
    */
    @Insert("""
            insert into ${tableInfo.tableName}
            (<#list tableFields as tableField>${tableField.columnName}<#if tableField_has_next>,</#if> </#list>)
            values(<#list tableFields as tableField> <#noparse>#{</#noparse>${tableField.fieldName}}<#if tableField_has_next>,</#if></#list>)
            """)
    public int insert(${tableInfo.firstUpperEntityName} entity);

    /**
    * 修改
    * @param entity
    * @return
    */
    @Update("""
    <script>
        update ${tableInfo.tableName}
        <set>
    <#list tableFields as tableField>
        <#if tableField.fieldName != "dataId">
            <if test="${tableField.fieldName} !=null ">${tableField.columnName}<#noparse>=#{</#noparse>${tableField.fieldName}}<#if tableField_has_next>,</#if></if>
        </#if>
    </#list>
        </set>
        where data_id=<#noparse>#{</#noparse>dataId}
    </script>
    """)
    public int update(${tableInfo.firstUpperEntityName} entity);

    /**
    * 更新状态
    * @param dataId
    * @param status
    * @return
    */
    @Update("update ${tableInfo.tableName} set status= <#noparse>#{</#noparse>status} where data_id = <#noparse>#{</#noparse>dataId}")
    public int changeStatus(@Param("dataId")Long dataId,@Param("status")String status);

    /**
    * 通过主键删除
    * @param dataId
    * @return
    */
    @Delete("delete from ${tableInfo.tableName} where data_id = <#noparse>#{</#noparse>dataId}")
    public int deleteById(Long dataId);

    /**
    * 通过主键删除
    * @param dataIds
    * @return
    */
    @Delete("""
        <script>
        delete from ${tableInfo.tableName} where data_id in
        <foreach item="dataId" collection="array" open="(" separator="," close=")">
            <#noparse>#{</#noparse>dataId}
        </foreach>
        </script>
    """)
    public int deleteByIds(Long [] dataIds);

    /**
    * 通过唯一索引查询
    * @param dataId
    * @return
    */
    @Select("select * from ${tableInfo.tableName} where data_id = <#noparse>#{</#noparse>dataId}")
    public  ${tableInfo.firstUpperEntityName} selectById(Long dataId);

    /**
    * 通过实体类查询集合
    * @param entity
    * @return
    */
    @Select("""
    <script>
        select * from ${tableInfo.tableName}
        <where>
            <#list tableFields as tableField>
            <if test="${tableField.fieldName} != null and ${tableField.fieldName} != ''"> and ${tableField.columnName}=<#noparse>#{</#noparse>${tableField.fieldName}}</if>
            </#list>
        </where>
    </script>
    """)
    public List<${tableInfo.firstUpperEntityName}> selectList(${tableInfo.firstUpperEntityName} entity);
}
